/*

Copyright (c) 2006 gpx@niniu.com.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the name of the copright holder nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

using System;
using System.Collections.Generic;
using System.Text;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.GPS;
using ZoneFiveSoftware.Common.Visuals;
//using BENBUN.ExportCRS.Xml.Topografix.Gpx11;
using BENBUN.ExportCRS.Xml.Garmin;
namespace BENBUN.ExportCRS
{
    partial class GPSUtils
    {

        public CoursePoint_t[] CreateValleys(Trackpoint_t[] track)
        {

            List<CoursePoint_t> coursePoints = new List<CoursePoint_t>();
            List<int> valleys = new List<int>();
            
            if (BenbunPlugin.Instance.distanceUnits == "meter")
            {
                _valleyAsc = BenbunPlugin.Instance.valleySizeAsc;
                _valleyDec = BenbunPlugin.Instance.valleySizeDec;
            }
            else
            {
                _valleyAsc = BenbunPlugin.Instance.valleySizeAsc * 0.3048;
                _valleyDec = BenbunPlugin.Instance.valleySizeDec * 0.3048;
            }
          
            int start;
            start = 0;
            //First find any spot where the previous height was less and the next height is less
            bool falling = false;
            int valley = -1, count = track.Length;
            double lastHeight = track[start].AltitudeMeters;
            for (int i = start; i < count; i++)
            {
                if (!track[i].AltitudeMetersSpecified)
                    continue;

                if (track[i].AltitudeMeters < lastHeight)
                {
                    valley = i;
                    falling = true;
                }

                if (track[i].AltitudeMeters > lastHeight && falling)
                {
                    //found summit
                    valleys.Add(valley);
                    falling = false;
                }

                lastHeight = track[i].AltitudeMeters;
            }

            //Now find if the summit is really a summit
            foreach (int currentValley in valleys)
            {
                if (DetermineIfRealValley(track, currentValley))
                {
                    CoursePoint_t coursePoint = new CoursePoint_t();
                    coursePoint.Time = track[currentValley].Time;
                    coursePoint.Position = track[currentValley].Position;
                    coursePoint.AltitudeMeters = track[currentValley].AltitudeMeters;
                    coursePoint.AltitudeMetersSpecified = true;
                    coursePoint.Name = "Valley";
                    coursePoint.PointType = CoursePointType_t.Valley;

                    coursePoints.Add(coursePoint);
                }
            }

            return coursePoints.ToArray();
        }

        private bool DetermineIfRealValley(Trackpoint_t[] track, int candidate)
        {
            double valleyHeight = track[candidate].AltitudeMeters;
            double lastHeight = valleyHeight;
            double leftAscent = 0.0;
            bool valleyFound = false;
            for (int i = candidate - 1; i >= 0; i--)
            {
                if (!track[i].AltitudeMetersSpecified)
                {
                    continue;
                }
                if (track[i].AltitudeMeters <= valleyHeight)
                {
                    //Note the greater or equal than, this is used in situations where there
                    //are two peaks very near each other that have *exactly* the same height
                    //This loop looks back and so if there is a peak before this one with the 
                    //same height, that one wins. In the other loop is only a greater than.
                    return false;
                }
                leftAscent += track[i].AltitudeMeters - lastHeight;

                if (leftAscent > _valleyDec)
                {
                    valleyFound = true;
                    break;
                }

                lastHeight = track[i].AltitudeMeters;
            }

            if (!valleyFound)
            {
                return false;
            }

            lastHeight = valleyHeight;
            double rightAscent = 0.0;
            valleyFound = false;
            for (int i = candidate + 1; i < track.Length; i++)
            {
                if (!track[i].AltitudeMetersSpecified)
                {
                    continue;
                }

                if (track[i].AltitudeMeters < valleyHeight)
                {
                    return false;
                }
                rightAscent += track[i].AltitudeMeters - lastHeight;

                if (rightAscent > _valleyAsc)
                {
                    valleyFound = true;
                    break;
                }

                lastHeight = track[i].AltitudeMeters;
            }

            return valleyFound;
        }

    }
}
